package pa165.dominatingspecies.frontend.environment;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.enums.ElementType;
import pa165.dominating.species.tos.ElementTo;
import pa165.dominating.species.tos.EnvironmentTo;
import pa165.dominatingspecies.frontend.element.EnumListMultiple;

import pa165.dominatingspecies.frontend.pages.BasePage;
import pa165.dominatingspecies.service.impl.ElementServiceImpl;
import pa165.dominatingspecies.service.impl.EnvironmentServiceImpl;

/**
 *
 * @author Ondrej Prazak
 */
@PreAuthorize("hasRole('ADMIN')")
public class CreateEnvironmentPage extends BasePage {

    @SpringBean(name = "environmentService")
    private EnvironmentServiceImpl envService;
    @SpringBean(name = "elementService")
    private ElementServiceImpl elementService;
    private String description;
    private String name;
    private List<ElementType> selectedElems = new ArrayList<>();

    public CreateEnvironmentPage() {
        Form<CreateEnvironmentPage> createForm = new Form<>("createEnvironmentForm",
                new CompoundPropertyModel<>(this));
        add(createForm);
        createForm.add(new Label("nameLabel", new StringResourceModel("envNameLabel", this, null)));
        createForm.add(new TextField<String>("name"));
        createForm.add(new Label("descLabel", new StringResourceModel("envDescLabel", this, null)));
        createForm.add(new TextArea<String>("description"));
        createForm.add(new Label("elementsLabel", new StringResourceModel("selectElements", this, null)));

        createForm.add(new EnumListMultiple("elements", new PropertyModel(this, "selectedElems")));

        BackToListLink backToList = new BackToListLink("backToList");
        createForm.add(backToList);

        Button submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {
                boolean errorsFound = false;
                if (name == null) {
                    getSession().warn(new StringResourceModel("envNameEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (description == null) {
                    getSession().warn(new StringResourceModel("envDescEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (selectedElems.isEmpty()) {
                    getSession().warn(new StringResourceModel("envNoElemsSelectedWarn", this, null).getString());
                    errorsFound = true;
                }

                if (!errorsFound) {

                    EnvironmentTo env = new EnvironmentTo();
                    env.setDescription(description);
                    env.setName(name);

                    List<ElementTo> found = new ArrayList<>();
                    for (ElementType t : selectedElems) {
                        found.add(elementService.getElementToByType(t));
                    }
                    env.setElements(found);
                    boolean thrown = false;
                    try {
                        envService.createEnvironment(env);
                    } catch (DataAccessException e) {
                        getSession().warn(new StringResourceModel("envCreateFail", this, null).getString());
                        thrown = true;
                    }
                    if (!thrown) {
                        getSession().info(new StringResourceModel("envAddedSucc", this, null).getString());
                        setResponsePage(EnvironmentsPage.class);
                    }
                }
            }
        };
        createForm.add(submitButton);
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
